package com.mmc.manager.mmcListener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.mmc.manager.domain.Brand;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.Content;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 作者：李嘉浩
 * 时间：2024-09-11-15:13
 * 功能：
 */

@CanalEventListener
public class MMCCanalListener {

    @Autowired
    private RedisService redisService;

    // @InsertListenPoint(table = {"tb_content"})
    // // @ListenPoint(eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.UPDATE},table = "tb_brand")
    // public void brandInsert(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
    //     //获得插入后的字段
    //     List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
    //
    //     Content content = new Content();
    //
    //     for (CanalEntry.Column column : afterColumnsList) {
    //         String name = column.getName();
    //         String value = column.getValue();
    //         if (name.equals("id")){
    //             content.setId(Long.parseLong(value));
    //         }
    //         if (name.equals("category_id")){
    //             content.setCategoryId(Long.parseLong(value));
    //         }
    //         if (name.equals("title")){
    //             content.setTitle(value);
    //         }
    //         if (name.equals("url")){
    //             content.setUrl(value);
    //         }
    //         if (name.equals("pic")){
    //             content.setPic(value);
    //         }
    //         if (name.equals("status")){
    //             content.setStatus(value);
    //         }
    //         if (name.equals("sort_order")){
    //             content.setSortOrder(Long.parseLong(value));
    //         }
    //     }
    //     redisService.setCacheMapValue(CacheConstants.CONTENT_MAP,content.getId()+"",content);
    // }
    //
    //
    // @DeleteListenPoint(table = {"tb_content"})
    // public void brandDelete(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
    //     //获得插入后的字段
    //     List<CanalEntry.Column> afterColumnsList = rowData.getBeforeColumnsList();
    //     Content content = new Content();
    //
    //     for (CanalEntry.Column column : afterColumnsList) {
    //         String name = column.getName();
    //         String value = column.getValue();
    //         if (name.equals("id")){
    //             content.setId(Long.parseLong(value));
    //         }
    //         if (name.equals("category_id")){
    //             content.setCategoryId(Long.parseLong(value));
    //         }
    //         if (name.equals("title")){
    //             content.setTitle(value);
    //         }
    //         if (name.equals("url")){
    //             content.setUrl(value);
    //         }
    //         if (name.equals("pic")){
    //             content.setPic(value);
    //         }
    //         if (name.equals("status")){
    //             content.setStatus(value);
    //         }
    //         if (name.equals("sort_order")){
    //             content.setSortOrder(Long.parseLong(value));
    //         }
    //     }
    //     redisService.deleteCacheMapValue(CacheConstants.CONTENT_MAP,content.getId()+"");
    // }

    @InsertListenPoint(table = {"tb_brand"})
    public void onInsertEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        // 1.1 获取插入后的列字段列表
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        Brand brand = new Brand();
        // 1.2 代表添加的某一行的多个列(循环一条就得到一行数据)
        for (CanalEntry.Column column : afterColumnsList) {
            String name = column.getName();     // 列名
            String value = column.getValue();   // 插入的该列的值
            if(name.equals("id")){
                brand.setId(Long.parseLong(value));
            }
            if(name.equals("name")){
                brand.setName(value);
            }
            if(name.equals("first_char")){
                brand.setFirstChar(value);
            }
        }
    }

    @DeleteListenPoint(table = {"tb_brand"})
    public void onDeletePoint(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        // 2.1 得到删除前的数据
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
        // 2.2 遍历删除前的数据
        for (CanalEntry.Column column : beforeColumnsList) {
            String name = column.getName();
            if(name.equals("id")){
                String value = column.getValue();
            }
        }
    }
}
